home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / GETENV.ASM < prev    next >
Assembly Source File  |  1991-12-25  |  3KB  |  132 lines

  1. ;
  2.         extrn    PSP:word
  3.         extrn    sl_strdup:far
  4.         extrn    sl_free:far
  5.         extrn    sl_strupr:far
  6.         extrn    sl_strlen:far
  7. ;
  8. StdGrp        group    StdLib, StdData
  9. ;
  10. StdData        segment    para public 'sldata'
  11. ZeroByte    db    0
  12. StdData        ends
  13. ;
  14. stdlib        segment    para public 'slcode'
  15.         assume    cs:StdGrp,ds:nothing
  16. ;
  17. ;
  18. ; GetEnv-  On entry, ES:DI points at an environment variable name.
  19. ;       This routine copies that string, converts it to upper case
  20. ;       and then searches for that string in the environment space.
  21. ;       This routine requires that you declare and set up PSP in the
  22. ;       main program.  This isn't a problem since this routine uses
  23. ;       malloc which also requires PSP.
  24. ;
  25. ;       Returns pointer to environment variable in ES:DI if it finds
  26. ;       said variable.  Also returns carry clear in this case.
  27. ;       Returns the carry set if it could not find the environment
  28. ;       variable or if there was a memory allocation error.
  29. ;
  30.         public    sl_GetEnv
  31. sl_GetEnv    proc    far
  32.         pushf
  33.         push    ds
  34.         push    si
  35.         push    bp
  36.         push    cx
  37.         push    ax
  38.         cld
  39.  
  40. ; First, duplicate the string so we can play around with it:
  41.  
  42.         call    StdGrp:sl_strdup
  43.         jc    BadGetEnv
  44.  
  45.  
  46. ; Now, convert all the characters in the string to upper case:
  47.  
  48.         call    StdGrp:sl_strupr
  49.  
  50. ; Get the length of the string into cx:
  51.  
  52.         call    StdGrp:sl_strlen
  53.  
  54. ; Save ptr to name in DS:SI for later use:
  55.  
  56.         mov    si, es
  57.         mov    ds, si
  58.         mov    si, di
  59. ;
  60. ; Get the address of the environment string space:
  61. ;
  62.         mov    ax, seg PSP
  63.         mov    es, ax
  64.         mov    es, es:PSP        ;Get adrs of PSP
  65.         mov    es, es:[2ch]        ;Get adrs of env blk.
  66.  
  67. ; Okay, search the environment string space for our string
  68.  
  69.         push    cx
  70.         push    ds
  71.         push    si
  72.         mov    bp, sp
  73.         xor    di, di            ;Start at ES:[0]
  74.         jcxz    NoMatch
  75. CmpsLp:    repe    cmpsb                ;Does this entry match?
  76.         je    GotMatch
  77.  
  78. ; The current entry did not match, try the next one:
  79.  
  80.         mov    cx, 8000h        ;Save for next zero.
  81.         mov    al, 0
  82.     repne    scasb
  83.         cmp    byte ptr es:[di], 0    ;End of Env?
  84.         je    GotMatch
  85.  
  86.         mov    si, 0[bp]
  87.         mov    ds, 2[bp]
  88.         mov    cx, 4[bp]
  89.         jmp    CmpsLp
  90.  
  91.  
  92. ; If there are zero characters in the source string, just return a pointer
  93. ; to a zero byte.
  94.  
  95. NoMatch:    mov    ax, seg ZeroByte
  96.         mov    es, ax
  97.         mov    di, offset ZeroByte
  98.  
  99. ; Return to the caller with carry clear if no error.
  100.  
  101. GotMatch:    mov    ax, es            ;Save ptr to stuff after
  102.         mov    cx, di            ; the env string.
  103.  
  104.         pop    di            ;Free up the string.
  105.         pop    es
  106.         add    sp, 2            ;Pop other junk off stack.
  107.         call    StdGrp:sl_free        
  108.  
  109.         mov    es, ax            ;Restore pointer to the
  110.         mov    di, cx            ; environment string.
  111.         clc
  112.         pop    ax
  113.         pop    cx
  114.         pop    bp
  115.         pop    si
  116.         pop    ds
  117.         popf
  118.         ret
  119.  
  120. BadGetEnv:    pop    ax
  121.         pop    cx
  122.         pop    bp
  123.         pop    si
  124.         pop    ds
  125.         popf
  126.         stc
  127.         ret
  128. sl_getenv    endp
  129.  
  130. stdlib        ends
  131.         end
  132.